package org.iteam.xlz.core.service.message;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.core.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.messages.PushInfo;
import org.iteam.xlz.core.entity.messages.PushInfoRecord;

public class PushInfoService extends BaseService<PushInfo>{
	private static PushInfoService instance;
	private PushInfoService(){}
	
	public static PushInfoService getInstance(){
		if(null==instance){
			instance=new PushInfoService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from pushinfo");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public PushInfo findOne(String piid){
		try {
			return super.show("select * from pushinfo where piid=?",piid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new PushInfo();
		}
	}
	
	public PushInfo findIsCurrent(int pushtype){
		try {
			return super.show("select * from pushinfo where status=0 and pushtype=? and iscurrent=0",pushtype);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<PushInfo> query(long page,long rows){
		try {
			return super.query("select * from pushinfo where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<PushInfo>();
		}
	}
	
	public boolean record(PushInfoRecord record){
		SqlBuilder sql=SqlBuilder.createInsert("pushinforecord");
		sql.addField("pirid",UUID.randomUUID().toString());
		sql.addField("uid",record.getUid());
		sql.addField("piid",record.getPiid());
		sql.addField("createtime",DateUtil.currentTime());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean create(PushInfo model){
		Connection con=null;
		try {
			con=super.getConnection(false);
			int i=0,j=0;
			if(model.getIscurrent()==0){
				PushInfo df=super.show("select * from pushinfo where iscurrent=0 and pushtype=?",model.getPushtype());
				if(null==df){
					j=1;
				}else{
					SqlBuilder sql1=SqlBuilder.createUpdate("pushinfo");
					sql1.addField("iscurrent",1);
					sql1.addWhere("iscurrent",0);
					sql1.addWhere("pushtype",model.getPushtype());
					j=super.updates(con,sql1.getSql(),sql1.getParams());
				}
			}
			SqlBuilder sql=SqlBuilder.createInsert("pushinfo");
			sql.addField("piid",UUID.randomUUID().toString());
			sql.addField("content",model.getContent());
			sql.addField("pushtype",model.getPushtype());
			sql.addField("iscurrent",model.getIscurrent());
			sql.addField("status",model.getStatus());
			sql.addField("createtime",DateUtil.currentTime());
			sql.addField("creator",model.getCreator());
			i=super.updates(con,sql.getSql(), sql.getParams());
			if(i==1&&j==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean update(PushInfo model){
		Connection con=null;
		try {
			con=super.getConnection(false);
			int i=0,j=1;
			if(model.getIscurrent()==0){
				PushInfo df=super.show("select * from pushinfo where iscurrent=0 and pushtype=?",model.getPushtype());
				if(null!=df){
					SqlBuilder sql1=SqlBuilder.createUpdate("pushinfo");
					sql1.addField("iscurrent",1);
					sql1.addWhere("iscurrent",0);
					sql1.addWhere("pushtype",model.getPushtype());
					j=super.updates(con,sql1.getSql(),sql1.getParams());
				}
			}
			SqlBuilder sql=SqlBuilder.createUpdate("pushinfo");
			sql.addField("content",model.getContent());
			sql.addField("pushtype",model.getPushtype());
			sql.addField("iscurrent",model.getIscurrent());
			sql.addField("status",model.getStatus());
			sql.addWhere("piid",model.getPiid());
			i=super.updates(con,sql.getSql(), sql.getParams());
			if(i==1&&j==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
}
